<?php
namespace app\admin\controller;

use app\admin\controller\Base;
use think\Session;
use think\Captcha;
use think\Geetestlib;

class Login extends Base
{
	public function __construct(){
		parent::__construct();
	}

	public function index() {
//        $this->view->engine->layout(false);
        return $this->fetch();
	}

	public function login() {
		// 判断表单是否提交
		if ($this->request->isPost()){
            // 验证码检验
           if (!empty(input('verify'))) {
               $verify = input('verify');
               if (!$this->check_verify($verify)) {
                   return $this->error("验证码错误！",url('/admin/Login/index'));
               }
           }else {
               return $this->error("验证码不能为空！",url('admin/Login/index'));
           }
    		$data=input();
            // 调用BasecheckPwd检验密码正确性
    		$data=$this->checkPwd($data['username'], $data['password'], $data['usertype']);
    		if ($data) {
    			Session::set('userinfo',$data);// 保存Session值
    			return  $this->success('登陆成功!',url('admin/Index/index'));
    		}
    		return $this->error('用户不存在或者密码错误!',url('admin/Login/index'));
    	}else {
    		 return $this->fetch();
    	}
	}

	// 退出
	public function logout()
    {
        // 退出登录删除全部session
        session(null);
        return $this->success('正在退出…',url('admin/Login/index'));
    }

    /**
    * geetest检测验证码
    */
    function geetest_chcek_verify($data){
        $geetest_id='6744411dbdbb218c59b4c2cab989837a';
        $geetest_key='29f1f71ac81b0fd481d788f04c116f63';
        $geetest=new \think\Geetestlib\Geetest($geetest_id,$geetest_key);
        $user_id=$_SESSION['geetest']['user_id'];
        if ($_SESSION['geetest']['gtserver']==1) {
            $result=$geetest->success_validate($data['geetest_challenge'], $data['  geetest_validate'], $data['geetest_seccode'], $user_id);
            if ($result) {
                return true;
            } else{
                return false;
            }
        }else{
            if ($geetest->fail_validate($data['geetest_challenge'],$data['geetest_validate'],$data['geetest_seccode'])) {
                return true;
            }else{
                return false;
            }
        }
    }

    /**
     * geetest生成验证码
     */
    public function geetest_show_verify(){
        $geetest_id='6744411dbdbb218c59b4c2cab989837a';
        $geetest_key='29f1f71ac81b0fd481d788f04c116f63';
        $geetest=new \think\Geetestlib\Geetest($geetest_id,$geetest_key);
        $user_id = "test";
        $status = $geetest->pre_process($user_id);
        $_SESSION['geetest']=array(
            'gtserver'=>$status,
            'user_id'=>$user_id
            );
        echo $geetest->get_response_str();
    }

    /**
     * geetest submit 提交验证
     */
    public function geetest_submit_check(){
        $data=input('post.');
        if (geetest_chcek_verify($data)) {
            echo '验证成功';
        }else{
            echo '验证失败';
        }
    }

    /**
     * geetest ajax 验证
     */
    public function geetest_ajax_check(){
        $data=input('post.');
        echo intval(geetest_chcek_verify($data));
    }

	// 验证码的生成及配置参数
    public function verify(){
    	$captcha = new \think\captcha\Captcha();
    	$captcha->imageH = 40;
    	$captcha->imageW = 140;
		$captcha->fontSize = 20;
		$captcha->length   = 4;
		$captcha->useNoise = false;
		return $captcha->entry();
    }

    // 检验验证码是否正确
    public function check_verify($code){
        $captcha = new \think\captcha\Captcha();
        return $captcha->check($code);
    }

}